﻿using System;
using System.Data;

using vtech_formBase;
using vtech_frameWorks.Exceptions;

namespace HospitalManagement.Clinics
{
    public partial class frmEditClinics : frmEdit
    {
        #region Delegate
        public delegate void EditDelegate();
        public event EditDelegate OnEdit = null;

        protected override void FireEvent()
        {
            if (OnEdit != null)
            {
                OnEdit();
            }
        }
        #endregion

        public frmEditClinics()
        {
            InitializeComponent();
        }

        public frmEditClinics(Guid ClinicsID)
        {
            InitializeComponent();
            ID = ClinicsID;
        }

        protected override void LoadData()
        {
            try
            {
                DataRow[] drow = ClsClinicsBS.GetAllClinics().Select(String.Format("ClinicsID='{0}'", ID));
                foreach (DataRow row in drow)
                {
                    txtClinicsCode.Text = row["ClinicsCode"].ToString();
                    txtClinicsName.Text = row["ClinicsName"].ToString();
                    txtDescription.Text = row["Description"].ToString();
                    if (row["Inactive"].ToString().Equals("True"))
                    {
                        chkInactive.Checked = true;
                    }
                    else
                    {
                        chkInactive.Checked = false;
                    }
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        protected override bool CheckControl()
        {
            try
            {
                errorProvider.Dispose();
                if (txtClinicsCode.Text == "")
                {
                    errorProvider.SetError(txtClinicsCode, "Bạn chưa nhập mã phòng khám. Xin vui lòng kiểm tra lại");
                    return false;
                }
                else
                {
                    if (txtClinicsName.Text == "")
                    {
                        errorProvider.SetError(txtClinicsName, "Bạn chưa nhập tên phòng khám. Xin vui lòng kiểm tra lại");
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
                return false;
            }
        }

        protected override void Edit()
        {
            try
            {
                using (DsClinics ds = new DsClinics())
                {
                    DataRow drow = ds.DtClinics.NewRow();
                    drow["ClinicsID"] = ID;
                    drow["ClinicsCode"] = txtClinicsCode.Text.Trim();
                    drow["ClinicsName"] = txtClinicsName.Text.Trim();
                    drow["Description"] = txtDescription.Text.Trim();
                    drow["Inactive"] = 0;
                    ds.DtClinics.Rows.Add(drow);
                    
                    ClsClinicsBS.DoEdit(ds.DtClinics);
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        private void frmEditClinics_Load(object sender, EventArgs e)
        {
            LoadData();
        }

    }
}
